﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Initialsettings();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Initialsettings()
        {
            string[] ports = SerialPort.GetPortNames();
            if (ports.Length > 0)
            {
                cmb_port.DataSource = ports;//获得端口号
                cmb_port.SelectedIndex = 0;
            }

            string[] bauds = new string[] { "自定义","4800", "9600", "115200" };
            cmb_baud.DataSource = bauds;//获取波特率；
            cmb_baud.SelectedIndex = 2;

            int[] data = new int[] { 5, 6, 7, 8 };
            cmb_databits.DataSource = data;//获取数据；
            cmb_databits.SelectedIndex = 3;

            cmb_parity.DataSource = Enum.GetNames(typeof(Parity));
            cmb_parity.SelectedIndex = 0;//校验位

            cmb_stopbits.DataSource = Enum.GetNames(typeof(StopBits));
            cmb_stopbits.SelectedIndex = 1;//停止位

        }
        private SerialPort serialPort = new SerialPort();//端口初始化
        private void lianjie_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort.PortName = cmb_port.Text.Trim();
                serialPort.BaudRate = Convert.ToInt32(cmb_baud.Text.Trim());//trim是去掉空格
                serialPort.DataBits = Convert.ToInt32(cmb_databits.Text.Trim());
                serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), cmb_parity.Text.Trim());
                serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmb_stopbits.Text.Trim());
                serialPort.Open();
                MessageBox.Show("串口打开成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

         private void Form1_Load(object sender, EventArgs e)
         {
           



         }

        private void port_datareceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!radioButton3.Checked)//如果接收方式是字符串
            {
                string str = serialPort.ReadExisting();//字符串读取
                textBox2.AppendText(str);//尾部添加
            }
            else
            {
                

                byte data;
                data = (byte)serialPort.ReadByte();//将int类型的数据强项转换byte
                string str = Convert.ToString(data, 16); //转换大写16进制
                textBox2.AppendText(str.Length == 1 ? "0" + str : str + " ");


            }
        }

        

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort.DataReceived += new SerialDataReceivedEventHandler(port_datareceived);

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
